using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using QLPMT_DTO;
namespace QLPMT_DAO
{
   public class LogInDAO
    {
        DataProvider Provider;
        public void config(string host, string db_name, string db_user, string db_pass)
        {
            DataProvider.host = host;
            DataProvider.db_name = db_name;
            DataProvider.db_user = db_user;
            DataProvider.db_pass = db_pass;

        }
        public LogInDAO()
        {
            Provider = new DataProvider();
        }
        public LogInDAO(string host, string db_name, string db_user, string db_pass)
        {
            Provider = new DataProvider(host, db_name, db_user, db_pass);
        }
       // Thay doi quyen su dung 
        public void Update(LogIn dto)
        {
            Provider.Connect();
            string sql = "update Login set MaPhanQuyen='" + dto.Maphanquyen + "', PassWord='" + dto.Pass + "' where username='" + dto.User + "'";
            Provider.ExecuteQuery(sql);
            Provider.Disconnect();
        }
        // Them 1 nguoi dung moi
        public void Insert(LogIn dto)
        {
            Provider.Connect();
            string sql = "insert into LogIn values('"+dto.User+"','"+dto.Pass+"','"+dto.Maphanquyen+"')";
            Provider.ExecuteQuery(sql);
            Provider.Disconnect();
        }
        public void delete(LogIn dto)
        {
            Provider.Connect();
            string sql = "delete LogIn where username='" + dto.User + "'";
            Provider.ExecuteQuery(sql);
            Provider.Disconnect();
        }
       
       // Danh Sach Nguoi Dung
       public List<LogIn> GetAll()
       {
           Provider.Connect();
           string sql = "select * from Login";
           SqlDataReader reader = Provider.ExecuteReader(sql);
           List<LogIn> list = new List<LogIn>();
           while (reader.Read())
           {
               string user = reader.GetString(0);
               string pass = reader.GetString(1);
               string ma = reader.GetString(2);
               LogIn dto = new LogIn(user, pass, ma);
               list.Add(dto);
           }
           Provider.Disconnect();
           return list;
       }
    }
}
